
#import "Main.h"
#import <appkit/Application.h>
#import <appkit/Listener.h>
#import <appkit/Panel.h>
#import <appkit/Speaker.h>

extern int    chdir(const char *);
extern int    system(const char *);
extern char * strcpy(char *,const char *);
extern size_t strlen(const char *);

char *stlchr(const char *,int);

id  infoPanel=nil;
id  helpPanel=nil;
BOOL appShouldQuit=NO;

@implementation Main

- info:sender
{
	if (!infoPanel) {
		infoPanel = [NXApp loadNibSection:"Info.nib" owner:self withNames:NO];
		[infoPanel useOptimizedDrawing:YES];
	}
	[infoPanel makeKeyAndOrderFront:self];
	return self;
}

- help:sender
{
	if (!helpPanel) {
		helpPanel=[NXApp loadNibSection:"Help.nib" owner:self withNames:NO];
		[helpPanel useOptimizedDrawing:YES];
	}
	[helpPanel makeKeyAndOrderFront:self];
	return self;
}

- (BOOL)appAcceptsAnotherFile:sender
{
	return YES;
}

- (int)app:sender openFile:(const char *)filename type:(const char *)aType
{
	char buffer[1000];

	sprintf(buffer, "tar -xf %s", filename);
	chdir(stlchr(filename,'/'));
	system(buffer);

	appShouldQuit=YES;
	return YES;
}

- appDidInit:sender
{
	if (appShouldQuit) {
		port_t workspacePort = NXPortFromName(NX_WORKSPACEREQUEST,NULL);

		if (workspacePort != PORT_NULL) {
			[[NXApp appSpeaker] setSendPort:workspacePort];
			[[NXApp appSpeaker] selectorRPC:"updateBrowser:" paramTypes:"@", sender];
		}
		[NXApp terminate:sender];
	}
	return self;
}

@end

char *stlchr(const char *name,int chr)
{
	static char	buffer[1000];
	int			x;

	strcpy(buffer,name);
	for (x = strlen(buffer); x; x--)
		if (buffer[x] == chr) {
			buffer[x] = '\0';
			break;
		}
	return (buffer); 
}
